using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace BumbleGame
{
    class Bug : AnimatedTexture
    {
        private float fTime, scrollY, oldXpos, oldYpos;
        private int screenheight;
        private int bugLife;
        public bool isFly, isDragonfly, isGrasshopper, isButterfly, isPacer, isFrog, isFireFly;
        public bool dying, dead;
        public bool isVisible;
        //private Random rndX, rndY;
        //private float rndfX, rndfY
        //Firefly stuff
        private AnimatedTexture flyWings, fireflyButt, fireflyButtTwinkle;
        private int randomCounter, xAdd, yAdd;
        private Random xRand, yRand;
        public bool isPickUpActive;

        private bool isFallingDown; //for grasshopper to leap and fall
        
        private bool isCheckingDamage; //For bugs that aren't always hitting the pig to tell when they should be
        
        private bool isLefttoRight, isOnFlower; //For checking which direction the butterfly is flying across the screen, and whether its resting on a flower
        //***pacer bug variables
        private bool seekingFlowerMode, homingMode, patrolMode; //for pacer, only true until pacer bug attaches to nearest flower

        //changed pacingx&y to this vector2
        private Vector2 pacingSpeed, radialDistanceVector;

        //hypotenuse of pacingspeed
        private float pacingTopSpeed, radialDistanceScalar, patrolBoundryRadius, radialDistanceDirection;

        //keeps track of which flower this bug is tethered to
        private int flowerIndex, angleVariation;

        Random autoRandom;

        //***end pacer bug variables
        
        //froggy related junk
        private AnimatedTexture tongue;
        private bool isLicking;
        private HitBox2D tongueBox;
        private float tongueBoxWidth;

        public Bug(int height, string BugType, float scrollSpeed, GraphicsDeviceManager graphics, ContentManager content)
        {
            screenheight = height;
            fTime = 0;
            isVisible = false;
            isFallingDown = false;
            isPickUpActive = false;
            scrollY = scrollSpeed;
            //autoseed the randomizer
            autoRandom = new Random();            

            switch (BugType)
            {
                case "fly":
                    isFly = true;
                    bugLife = 1;
                    flyWings = new AnimatedTexture();
                    flyWings.Load(graphics.GraphicsDevice, content, "Images/Enemies/bug_fly_wing_3of8", 8, 1, 30, 0, 2, true);
                    isCheckingDamage = true;
                    SetHitBoxOffset(65);
                    break;
                case "dragonfly":
                    isDragonfly = true;
                    bugLife = 1;
                    isCheckingDamage = true;
                    SetHitBoxOffset(50);
                    break;
                case "grasshopper":
                    isGrasshopper = true;
                    SpriteScale = 0.5f;
                    bugLife = 1;
                    isCheckingDamage = false;
                    SetHitBoxOffset(40);
                    break;
                case "butterfly":
                    isButterfly = true;
                    bugLife = 1;
                    isCheckingDamage = true;
                    SetHitBoxOffset(60);
                    break;
                case "pacer":
                    isPacer = true;
                    bugLife = 1;
                    seekingFlowerMode = true;
                    homingMode = false;
                    patrolMode = false;
                    patrolBoundryRadius = 100.0f;
                    pacingSpeed.X = 1.0f;
                    pacingSpeed.Y = 1.0f;
                    pacingTopSpeed = 1.41f;
                    isCheckingDamage = true;
                    SetHitBoxOffset(45);
                    break;
                case "frog":
                    isFrog = true;
                    bugLife = 1;
                    tongue = new AnimatedTexture();
                    tongue.Load(graphics.GraphicsDevice, content, "Images/Enemies/frogT", 1, 1, 1, 0, 0, true);
                    tongue.recWidth = 0;                    
                    isCheckingDamage = true;
                    SetHitBoxOffset(30);
                    break;
                case "firefly":
                    isFireFly = true;
                    bugLife = 1;
                    flyWings = new AnimatedTexture();
                    flyWings.Load(graphics.GraphicsDevice, content, "Images/Enemies/bug_firefly_wingstrip", 4, 1, 60, 0, 3, true);
                    fireflyButt = new AnimatedTexture();
                    fireflyButt.Load(graphics.GraphicsDevice, content, "Images/Enemies/bug_firefly_buttstrip", 4, 1, 8, 0, 3, true);
                    fireflyButtTwinkle = new AnimatedTexture();
                    fireflyButtTwinkle.Load(graphics.GraphicsDevice, content, "Images/Enemies/bug_firefly_twinklestrip", 4, 1, 14, 0, 3, false);
                    isCheckingDamage = true;
                    SpriteScale = 0.85f;
                    SetHitBoxOffset(65);
                    yRand = new Random(0);
                    xRand = new Random(1);
                    xAdd = 2;
                    yAdd = 2;
                    randomCounter = 0;                    
                    break;
                default:
                    isFly = true;
                    bugLife = 1; 
                    flyWings = new AnimatedTexture();
                    flyWings.Load(graphics.GraphicsDevice, content, "Images/Enemies/bug_fly_wing_3of8", 8, 1, 30, 0, 2, true);
                    isCheckingDamage = true;
                    SetHitBoxOffset(65);
                    break;
            }
            dying = false;
            dead = false;
        }

        //Scroll the bug at entry time
        public void UpdateBug(float elapsed, Bumblepig bumblepig, List<Flower> femaleFlowers)
        {
            //update bug at entryTime

            if (SpritePosition.Y >= (0 - ((aTexture.Height / framesTall) / 2)))
            {
                isVisible = true;
                if (isVisible)
                {
                    UpdateFrame(elapsed);
                    SpritePosition.Y += scrollY;
                    fTime += elapsed;                    

                    if (!dying)
                    {
                        #region fly
                        if (isFly)
                        {
                            flyWings.UpdateFrame(elapsed);
                            //AI to move horizontally towards bumblepig... 
                            if (SpritePosition.Y < bumblepig.spritePosition.Y && bumblepig.BumbleBody.SpriteLayer == 1)
                            {
                                if (SpritePosition.X > bumblepig.spritePosition.X)
                                {
                                    if (SpritePosition.X - bumblepig.spritePosition.X < 200.0f)
                                    { SpritePosition.X -= (SpritePosition.X - bumblepig.spritePosition.X) / 50; } //moving him towards pig
                                    else
                                    { SpritePosition.X -= (SpritePosition.X - bumblepig.spritePosition.X) / 75; }
                                }
                                if (SpritePosition.X < bumblepig.spritePosition.X)
                                {
                                    if (bumblepig.spritePosition.X - SpritePosition.X < 200.0f)
                                    { SpritePosition.X += (bumblepig.spritePosition.X - SpritePosition.X) / 50; } //moving him towards pig
                                    else
                                    { SpritePosition.X += (bumblepig.spritePosition.X - SpritePosition.X) / 75; }
                                }
                            }
                            else //straightening out when past pig, facing pig, or can't see pig
                            {
                                if (SpriteRotation <= 0f)
                                {
                                    SpriteRotation += 0.1f;
                                }
                                if (SpriteRotation >= 0f)
                                {
                                    SpriteRotation -= 0.1f;
                                }
                            }

                            if (SpriteRotation < 0.01f && SpriteRotation > -0.01f) //lock to zero if close
                            {
                                SpriteRotation = 0f;
                            }                            
                        }
                        #endregion fly

                        #region grasshopper
                        if (isGrasshopper)
                        {
                            if (fTime > 1.0f)//sit on the ground for a second
                            {
                                if (!isFallingDown)//then leap
                                {
                                    SpriteScale += 0.02f; //maybe figure out some multiplier to fake a little gravity
                                    if (SpriteScale > 1.5f) SpriteScale = 1.5f;
                                    if (SpriteScale == 1.5f) isFallingDown = true; //he reached the top, now drop
                                }
                                else //he's falling from the leap
                                {
                                    SpriteScale -= 0.02f;
                                    if (SpriteScale < 0.5f) SpriteScale = 0.5f;
                                    if (SpriteScale == 0.5f)
                                    {
                                        isFallingDown = false;
                                        fTime = 0.0f;
                                    }
                                    foreach (Flower flower in femaleFlowers)
                                    { //if he lands on a flower, stay up there
                                        if (flower.BigHitBox.Intersects(this.HitBox2) || flower.BigHitBox.Contains(this.HitBox2))
                                        {

                                            if (SpriteScale < 1.0f)
                                            {
                                                SpriteScale = 1.0f;
                                                isFallingDown = false;
                                                fTime = 0.0f;
                                            }
                                        }
                                    }
                                }
                                SpritePosition.Y += 2.0f; //go a little faster while jumping
                                if (SpriteScale >= 1.0f) isCheckingDamage = true; //only check damage while up in the air or on a flower
                                else isCheckingDamage = false;
                            }
                        }
                        #endregion grasshopper

                        #region butterfly
                        if (isButterfly) //need to figure out how to give them variety in starting direction and angle
                        {
                            if (isOnFlower)
                            {
                                //put in some info about switching the first and last frames of animation
                                Pause();
                                if (fTime > 2.0f) //sit on the flower for a couple seconds, then take off in the other direction
                                {
                                    isOnFlower = false;
                                    if (isLefttoRight) isLefttoRight = false;
                                    else isLefttoRight = true;
                                    fTime = 0.0f;
                                }
                            }
                            else
                            {
                                Play();
                                if (isLefttoRight) SpritePosition.X += 1.5f;
                                else SpritePosition.X -= 1.5f;
                                SpritePosition.Y += 0.5f; //go a little faster while flying
                                if (fTime > 2.0f)
                                {
                                    foreach (Flower flower in femaleFlowers)
                                    {
                                        if (flower.BigHitBox.Intersects(this.HitBox2) || flower.BigHitBox.Contains(this.HitBox2))
                                        {
                                            isOnFlower = true;
                                            fTime = 0.0f;
                                        }
                                    }
                                }
                            }
                        }
                        #endregion butterfly

                        #region pacer
                        if (isPacer)
                        {
                            //search for nearest flower, and lock on if it is close enough
                            if (seekingFlowerMode)
                            {
                                for (int i = 0; i < femaleFlowers.Count; i++)//Flower flower in femaleFlowers)
                                {
                                    if (femaleFlowers[i].BigHitBox.Intersects(HitBox2))
                                    {
                                        flowerIndex = i;
                                        seekingFlowerMode = false;
                                        homingMode = true;
                                        break;
                                    }
                                }
                            }

                            //if flower goes offscreen, bug resumes flower search
                            if ( femaleFlowers.Count < flowerIndex) //(homingMode || patrolMode) && !femaleFlowers[flowerIndex].isVisible)
                            {
                                homingMode = false;
                                patrolMode = false;
                                seekingFlowerMode = true;
                                pacingSpeed.X = 1.0f;
                                pacingSpeed.Y = 1.0f;
                            }

                            //move towards center of flower when target is acquired
                            if (homingMode)
                            {
                                //construct vector between pacer bug and the center of its assigned flower
                                radialDistanceVector = Vector2.Subtract(femaleFlowers[flowerIndex].flower.SpritePosition, this.SpritePosition);

                                //hang on to distance
                                radialDistanceScalar = radialDistanceVector.Length();

                                //change vector to unit vector, magnitude is changed to 1 while maintaining the same direction
                                //freaks the hell out if the vector magnitude is zero, try not to do that
                                radialDistanceVector.Normalize();

                                //multiply it by the desired speed so the bug travels at a constant velocity
                                pacingSpeed = Vector2.Multiply(radialDistanceVector, pacingTopSpeed);


                                //if close enough to the center, switch to patrol mode
                                if (patrolBoundryRadius > radialDistanceScalar)
                                {
                                    homingMode = false;
                                    patrolMode = true;
                                }
                            }

                            //when close enough to center, start to patrol
                            if (patrolMode)
                            {
                                //construct vector between pacer bug and the center of its assigned flower
                                radialDistanceVector = Vector2.Subtract(femaleFlowers[flowerIndex].flower.SpritePosition, this.SpritePosition);

                                //hang on to distance
                                radialDistanceScalar = radialDistanceVector.Length();

                                //if boundry reached then turn around in a slightly random fashion
                                if (patrolBoundryRadius < radialDistanceScalar)
                                {
                                    radialDistanceDirection = (float)Math.Atan2(radialDistanceVector.Y, radialDistanceVector.X);

                                    //could go 45 degrees left or right of center, need to convert to radians

                                    angleVariation = autoRandom.Next(-45, 45);


                                    //add this to the direction angle of the distance vector and convert anglevariation to radians
                                    this.SpriteRotation = radialDistanceDirection + (angleVariation * ((float)Math.PI / 180));

                                    //then calculate speed with this new angle and the pacertopspeed as the hypotenuse

                                    pacingSpeed.X = pacingTopSpeed * (float)Math.Cos(SpriteRotation);
                                    pacingSpeed.Y = pacingTopSpeed * (float)Math.Sin(SpriteRotation);

                                }
                            }
                            SpritePosition.Y += pacingSpeed.Y;
                            SpritePosition.X += pacingSpeed.X;
                        }
                        #endregion pacer

                        #region firefly
                        if (isFireFly)
                        {
                            flyWings.UpdateFrame(elapsed);
                            fireflyButt.UpdateFrame(elapsed);
                            fireflyButtTwinkle.UpdateFrame(elapsed);
                            //AI to move horizontally towards bumblepig... 
                            if (SpritePosition.Y < bumblepig.spritePosition.Y && bumblepig.BumbleBody.SpriteLayer == 1)
                            {
                                if (SpritePosition.X > bumblepig.spritePosition.X)
                                {
                                    if (SpritePosition.X - bumblepig.spritePosition.X < 200.0f)
                                    { SpritePosition.X -= (SpritePosition.X - bumblepig.spritePosition.X) / 50; } //moving him towards pig
                                    else
                                    { SpritePosition.X -= (SpritePosition.X - bumblepig.spritePosition.X) / 75; }
                                }
                                if (SpritePosition.X < bumblepig.spritePosition.X)
                                {
                                    if (bumblepig.spritePosition.X - SpritePosition.X < 200.0f)
                                    { SpritePosition.X += (bumblepig.spritePosition.X - SpritePosition.X) / 50; } //moving him towards pig
                                    else
                                    { SpritePosition.X += (bumblepig.spritePosition.X - SpritePosition.X) / 75; }
                                }
                            }
                            else //straightening out when past pig, facing pig, or can't see pig
                            {
                                if (SpriteRotation <= 0f)
                                {
                                    SpriteRotation += 0.1f;
                                }
                                if (SpriteRotation >= 0f)
                                {
                                    SpriteRotation -= 0.1f;
                                }
                            }

                            if (SpriteRotation < 0.01f && SpriteRotation > -0.01f) //lock to zero if close
                            {
                                SpriteRotation = 0f;
                            }
                        }
                        #endregion firefly

                        //this little line corrects the bug's rotation so that it always faces the direction it's moving
                        if (isPacer) SpriteRotation = -(float)Math.Atan2((SpritePosition.X - oldXpos), (SpritePosition.Y - oldYpos) - scrollY);
                        else SpriteRotation = -(float)Math.Atan2((SpritePosition.X - oldXpos), (SpritePosition.Y - oldYpos));
                        oldXpos = SpritePosition.X;
                        oldYpos = SpritePosition.Y;
                    }
                    //extra stuff that needs to keep updating with the bugs after they're dying
                    if (isFly)
                    {
                        flyWings.SpritePosition = SpritePosition;
                        flyWings.SpriteRotation = SpriteRotation;
                        flyWings.SpriteScale = SpriteScale;
                    }
                    //Firefly stuff to keep his wings, butt, and buttTwinkle all together
                    if (isFireFly)
                    {
                        flyWings.SpritePosition = SpritePosition;
                        flyWings.SpriteRotation = SpriteRotation;
                        flyWings.SpriteScale = SpriteScale;

                        if (!dying)
                        {
                            fireflyButt.SpritePosition = SpritePosition;
                            fireflyButt.SpriteRotation = SpriteRotation;
                            fireflyButt.SpriteScale = SpriteScale;

                            fireflyButtTwinkle.SpritePosition = SpritePosition;
                            fireflyButtTwinkle.SpriteRotation = SpriteRotation;
                            fireflyButtTwinkle.SpriteScale = SpriteScale;
                        }

                    }
                    //frog is down here cause he doesn't die... so he needs to keep updating
                    #region frog
                    if (isFrog)
                    {
                        if (fTime > 2.0f)
                        {
                            isLicking = true;
                            fTime = 0.0f;
                        }
                        if (isLicking)
                        {
                            //now lets try this growing thing
                            if (fTime < 0.6f)
                            {
                                tongue.recWidth = (fTime / 0.6f) * tongue.aTexture.Width;
                                tongueBoxWidth = (fTime / 0.6f) * tongue.aTexture.Width;
                            }
                            else 
                            {
                                tongue.recWidth = (1 -((fTime - 0.6f) / 0.6f)) * tongue.aTexture.Width;
                                tongueBoxWidth = (1 - ((fTime - 0.6f) / 0.6f)) * tongue.aTexture.Width;
                            }
                                                                                 
                            if (fTime > 1.2f)
                            {
                                fTime = 0.0f;
                                isLicking = false;
                            }                            
                            //hitbox for frog tongue
                            tongueBox = new HitBox2D(new Vector2 ((tongue.SpritePosition.X - (tongue.aTexture.Width / 2)), (tongue.SpritePosition.Y - (tongue.aTexture.Height / 2))), 
                                               new Vector2((tongue.SpritePosition.X - (tongue.aTexture.Width / 2) + tongueBoxWidth), (tongue.SpritePosition.Y + (tongue.aTexture.Height / 2))));
                        }
                        tongue.SpritePosition.Y = this.SpritePosition.Y - 15;
                        if (this.SpritePosition.X > 512) tongue.SpritePosition.X = this.SpritePosition.X - 200;
                        else tongue.SpritePosition.X = this.SpritePosition.X + 200;
                        tongue.UpdateFrame(elapsed);
                        tongue.Pause();
                    }
                    
                    #endregion frog
                }
            }

            if (SpritePosition.Y >= (screenheight + ((aTexture.Height / framesTall) / 2))) //once off screen, reset like its a new bug for next go round
            {
                isVisible = false;                
            }            
        }

        //overloaded function for geoff boss flies
        public void UpdateBug(float elapsed, Vector2 startingPosition)
        {
            //update bug at entryTime

            if (isVisible)
            {
                UpdateFrame(elapsed);

                if (!dying)
                {

                    if (isFly)
                    {
                        flyWings.UpdateFrame(elapsed);
                    }
                    //this little line corrects the bug's rotation so that it always faces the direction it's moving
                    SpriteRotation = -(float)Math.Atan2((SpritePosition.X - oldXpos), (SpritePosition.Y - oldYpos));
                    oldXpos = SpritePosition.X;
                    oldYpos = SpritePosition.Y;
                }
                //extra stuff that needs to keep updating with the bugs after they're dying
                if (isFly)
                {
                    flyWings.SpritePosition = SpritePosition;
                    flyWings.SpriteRotation = SpriteRotation;
                    flyWings.SpriteScale = SpriteScale;
                }
            }
            if (dead)
            {
                this.SpriteScale = 1f;
                this.SpritePosition = startingPosition;
                isVisible = true;
                bugLife = 1;
                dead = false;
                dying = false;
                isCheckingDamage = true;
            }
        }

        public void CheckDamage(Bumblepig bumblepig, SoundBank sfxBank, List<Bug> bugs)
        {
            if (isVisible && isCheckingDamage)
            {
                //if bug is a fly, check hit detection with bumblepig's hitbox as primary
                //if bug is a dragonfly, check hit detection with the dragonfly's hitbox as primary
                //better to keep the bigger hitbox detecting the smaller hitbox
                if (bumblepig.onTopLayer && (((isFly || isGrasshopper || isPacer ||isFireFly) && bumblepig.BumbleBody.HitBox2.Intersects(this.HitBox2))
                    || ((isDragonfly || isFrog || isButterfly)&& this.HitBox2.Intersects(bumblepig.BumbleBody.HitBox2))) && !this.dying && !bumblepig.invulnerable)
                {
                    bumblepig.HurtBumblepig(sfxBank);
                    if (isFireFly) isPickUpActive = true;
                    bugLife = 0;
                    if (bugLife == 0) //if bug has no more life, get to dying
                    {
                        this.dying = true;                        
                    }
                }
                if (!isFrog && !dying && !bumblepig.bulletDestroyed && this.HitBox2.Contains(bumblepig.Bullet.SpritePosition))
                {
                    bugLife = 0;
                    if (bugLife == 0) //if bug has no more life, get to dying
                    {
                        this.dying = true;
                    }
                    bumblepig.bulletDestroyed = true;
                }

                if (this.dying && !this.dead) //spectacular shrink and spin death
                {   //we need to put in different frames of animation for this dying business
                    if (isDragonfly)
                    {
                        SpriteScale -= 0.04f;
                    }
                    if (isFly || isPacer || isButterfly || isGrasshopper || isFireFly)
                    {
                        SpriteScale -= 0.02f;
                        SpriteRotation += 0.2f;                        
                    }
                    if (SpriteScale <= 0.02f) //once he's too small... dead
                    {
                        this.dead = true;
                        isVisible = false;                        
                    }
                }
                if (isFrog && isLicking) 
                {
                    if (bumblepig.onTopLayer && tongueBox.Intersects(bumblepig.BumbleBody.HitBox2) && !bumblepig.invulnerable)
                    {
                        bumblepig.HurtBumblepig(sfxBank);
                    }

                    foreach (Bug bug in bugs)
                    {
                        if (!bug.isFrog && bug.isVisible && tongueBox.Intersects(bug.HitBox2))
                        {
                            //bug.bugLife -= 1;
                            bug.dying = true;
                        }
                    }
                }
            }
        }

        //overloaded checkdamage function for geoff boss flies (vs. bumblepig and the lighter)
        public void CheckDamage(Bumblepig bumblepig, SoundBank sfxBank, HitBox2D LighterHitbox)
        {
            if (isVisible && isCheckingDamage)
            {
                //bumblepig hit detection
                if (bumblepig.onTopLayer && (((isFly) && bumblepig.BumbleBody.HitBox2.Intersects(this.HitBox2))) && !this.dying && !bumblepig.invulnerable)
                {
                    bumblepig.HurtBumblepig(sfxBank);

                    bugLife = 0;
                    if (bugLife == 0) //if bug has no more life, get to dying
                    {
                        this.dying = true;
                    }
                }

                //lighter hit detection
                if (!this.dying && this.HitBox2.Intersects(LighterHitbox))
                {
                    bugLife = 0;
                    if (bugLife == 0) //if bug has no more life, get to dying
                    {
                        this.dying = true;
                    }
                }


                //bullet hit detection
                if (!dying && !bumblepig.bulletDestroyed && this.HitBox2.Contains(bumblepig.Bullet.SpritePosition))
                {
                    bugLife = 0;
                    if (bugLife == 0) //if bug has no more life, get to dying
                    {
                        this.dying = true;
                    }
                    bumblepig.bulletDestroyed = true;
                }

                if (this.dying && !this.dead) //spectacular shrink and spin death
                {
                    if (isFly)
                    {
                        SpriteScale -= 0.02f;
                        SpriteRotation += 0.2f;
                    }
                    if (SpriteScale <= 0.02f) //once he's too small... dead
                    {
                        this.dead = true;
                        isVisible = false;
                        isCheckingDamage = false;
                    }
                }

            }
        }

        //overloaded fuction for geoff boss flies (vs. sprayclouds)
        public void CheckDamage(HitBox2D otherHitbox)
        {
            if (isVisible && isCheckingDamage)
            {


                //other object hit detection
                if (!this.dying && this.HitBox2.Intersects(otherHitbox))
                {
                    bugLife = 0;
                    if (bugLife == 0) //if bug has no more life, get to dying
                    {
                        this.dying = true;
                    }
                }

                if (this.dying && !this.dead) //spectacular shrink and spin death
                {
                    if (isFly)
                    {
                        SpriteScale -= 0.02f;
                        SpriteRotation += 0.2f;
                    }
                    if (SpriteScale <= 0.02f) //once he's too small... dead
                    {
                        this.dead = true;
                        isVisible = false;
                        isCheckingDamage = false;
                    }
                }
            }
        }

        public void CheckPowerUp(ref float fTotalConfidenceMeter, HitBox2D otherHitbox)
        {
            if (isFireFly && isPickUpActive)
            {
                //Firefly butt pickup after death
                
                if (randomCounter == 15)
                {
                    yAdd = yRand.Next(1, 4);
                }
                if (randomCounter == 30)
                {
                    yAdd = yRand.Next(1, 4);
                    xAdd = xRand.Next(-2, 3);
                    if (xAdd == 0) xAdd = -1;
                    randomCounter = 0;
                }
                fireflyButt.SpritePosition.Y += yAdd;
                fireflyButt.SpritePosition.X += xAdd;
                randomCounter++;

                if (fireflyButt.SpritePosition.Y >= (screenheight + ((aTexture.Height / framesTall) / 2))) isPickUpActive = false;

                if (fireflyButt.HitBox2.Intersects(otherHitbox))
                {
                    fTotalConfidenceMeter += 100;
                    isPickUpActive = false;
                }
            }
        }


        public void FlipTongue()
        {
            //tongue.spriteFX = SpriteEffects.FlipHorizontally; This inverts the image, but not the rectangle
            tongue.SpriteRotation = 3.14f; //This inverts the rectangle
            tongue.spriteFX = SpriteEffects.FlipVertically;            
        }


        public void Draw(SpriteBatch Batch)
        {
            //Draw bug if he's on the screen and not dead
            if (isVisible && !dead) 
            {
                if (isFrog) tongue.DrawFrame(Batch);
                if (isFireFly && !isPickUpActive) fireflyButt.DrawFrame(Batch);
                DrawFrame(Batch);
                if (isFly) flyWings.DrawFrame(Batch);
                if (isFireFly)
                {
                    flyWings.DrawFrame(Batch);                    
                    fireflyButtTwinkle.DrawFrame(Batch);
                }
            }
            if (isPickUpActive && isFireFly) fireflyButt.DrawFrame(Batch);
        }
    }
}
